package ngames.com.sushitv.temp.Interface;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.temp.Module.AnimeSeriesFactory;
import ngames.com.sushitv.temp.model.RankData;

/**
 * Created by edmond on 16-7-29.
 */
public class AnimeSeriesPresenter implements AnimeSeriesTaskInterface,AnimeSeriesFactory.OnLoadAnimeSeriesDataListListener {

    private AnimeSeriesFactory animeSeriesFactory;
    private AnimeSeriesInterface view;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Log.d("FeaturedPresenter", animeSeriesFactory.getBannerUrls().get(0));
                    view.initingData(animeSeriesFactory.getBannerUrls(), animeSeriesFactory.getAnimeSeriesArray());
                    view.initedData();
                    break;
                case 2:
                    view.refreshedData(animeSeriesFactory.getBannerUrls(), animeSeriesFactory.getAnimeSeriesArray());
                    break;
            }
        }
    };

    public AnimeSeriesPresenter(AnimeSeriesFactory animeSeriesFractory, AnimeSeriesInterface view){
        this.animeSeriesFactory = animeSeriesFractory;
        this.view = view;
    }

    @Override
    public void initData() {
        Context context= AppApplication.getContext();
        animeSeriesFactory.initData(handler);

        animeSeriesFactory.initDataTop(context, "H_D", this);
        animeSeriesFactory.initDataNewOngoingAnime(context, "H_0", this);
        animeSeriesFactory.initDataNewOngoingAnime(context,"H_1",this);
    }

    @Override
    public void refreshData() {
        view.refresinghData();
        animeSeriesFactory.refreshData(handler);
    }

    @Override
    public void initImage(ImageView timage, String cover) {
        animeSeriesFactory.getImage(timage,cover,AppApplication.getContext());
    }

    @Override
    public void onSuccess(List<RankData> list,String flag) {
        if(flag.equals("H_D")) {
            view.addTop(list);
        }else if(flag.equals("H_0")){
            view.addNewOngoingAnime(list);
        }else if(flag.equals("H_1")){
            view.addPopularCompletedAnime(list);
        }
    }

    @Override
    public void onFailure(String msg, Exception e) {

    }
}
